/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__protobuf_do_pack.c                              */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/12 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "../../comm/frcmd_parse_detail.h"
#include "../../comm/client.pb-c.h"
#include "../../comm/frcmd_parse.h"
#include "../../comm/apm.pb-c.h"
#include "../../comm/frcmd_client.h"
#include "../../comm/frcomm.h"
#include "../../comm/frmem.h"
#include "../../comm/frsutl.h"
#include "../../comm/apmtest_cunit.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

int __protobuf_do_pack(Command *p_st_cmd, char **pp_data, unsigned int *p_ui_out);


void case1(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    char *p_data = NULL;
    unsigned int ui_out = 0;
    L7Command *l7_cmd = NULL;
    L7Setting *l7_settings = NULL;

    //设置l7_settings
    l7_settings = frmalloc(sizeof(L7Setting));
    l7_setting__init(l7_settings);
    l7_settings->has_ip_proto = 1;
    l7_settings->ip_proto = 1;
    l7_settings->has_ip = 1;
    l7_settings->ip = iptools_parse_ipaddr("192.168.1.1");
    l7_settings->has_port = 1;
    l7_settings->port = 6536;
    l7_settings->l7_proto = frmalloc(2);
    snprintf(l7_settings->l7_proto,2,"%s","0");

    //设置l7_cmd
    l7_cmd = frmalloc(sizeof(L7Command));
    l7_command__init(l7_cmd);
    l7_cmd->action = frmalloc(strlen("add") + 1);
    snprintf(l7_cmd->action,strlen("add") + 1,"%s","add");
    l7_cmd->n_l7_settings = 1;
    l7_cmd->l7_settings = &l7_settings;

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->l7_cmd = l7_cmd;
    p_st_cmd->cmd_type = 1;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_data,&ui_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(l7_settings->l7_proto);
    frfree(l7_settings);
    frfree(l7_cmd->action);
    frfree(l7_cmd);
    frfree(p_st_cmd);
    frfree(p_data);
}

//filter
void case2(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    FilterCommand *filter_cmd = NULL;
    Filter *filters = NULL;
    char *p_data = NULL;
    unsigned int i_cmd_len = 0;
    int i_size_tmp = 0;

    //设置filters
    filters = frmalloc(sizeof(Filter));
    filter__init(filters);
    filters->ip_proto = 1;
    i_size_tmp = strlen("192.168.1.1") + 1;
    filters->ip_from = frmalloc(i_size_tmp);
    snprintf(filters->ip_from,i_size_tmp,"%s","192.168.1.1");

    i_size_tmp = strlen("192.168.1.2") + 1;
    filters->ip_to = frmalloc(i_size_tmp);
    snprintf(filters->ip_to,i_size_tmp,"%s","192.168.1.2");

    i_size_tmp = strlen("22") + 1;
    filters->port_from = frmalloc(i_size_tmp);
    snprintf(filters->port_from,i_size_tmp,"%s","22");

    i_size_tmp = strlen("6356") + 1;
    filters->port_to = frmalloc(i_size_tmp);
    snprintf(filters->port_to,i_size_tmp,"%s","6536");

    i_size_tmp = strlen("60:00:ef:00:ea:56") + 1;
    filters->mac = frmalloc(i_size_tmp);
    snprintf(filters->mac,i_size_tmp,"%s","60:00:ef:00:ea:56");

    //设置filter_cmd
    filter_cmd = frmalloc(sizeof(FilterCommand));
    filter_command__init(filter_cmd);
    filter_cmd->action = frmalloc(strlen("add") + 1);
    snprintf(filter_cmd->action,strlen("add") + 1,"%s","add");
    filter_cmd->n_filters = 1;
    filter_cmd->filters = &filters;

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->filter_cmd = filter_cmd;
    p_st_cmd->cmd_type = 2;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_data,&i_cmd_len);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(filters->ip_from);
    frfree(filters->ip_to);
    frfree(filters->port_from);
    frfree(filters->port_to);
    frfree(filters->mac);
    frfree(filters);
    frfree(filter_cmd->action);
    frfree(filter_cmd);
    frfree(p_st_cmd);
    frfree(p_data);

}


//java
void case3(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    JavaCommand *java_cmd = NULL;
    char *p_data = NULL;
    unsigned int i_cmd_len = 0;

    //设置java_cmd
    java_cmd = frmalloc(sizeof(JavaCommand));
    java_command__init(java_cmd);
    java_cmd->family = frmalloc(strlen("unix") + 1);
    snprintf(java_cmd->family,strlen("unix") + 1,"%s","unix");
    java_cmd->port = 22;

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->java_cmd = java_cmd;
    p_st_cmd->cmd_type = 3;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_data,&i_cmd_len);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(java_cmd->family);
    frfree(java_cmd);
    frfree(p_st_cmd);
    frfree(p_data);

}

//commit
void case4(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *commit_cmd = NULL;
    char *p_data = NULL;
    unsigned int i_cmd_len = 0;

    //设置commit_cmd
    commit_cmd = frmalloc(sizeof(CommitCommand));
    commit_command__init(commit_cmd);
    commit_cmd->action = 1;
    commit_cmd->cmd = 2;

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->commit_cmd = commit_cmd;
    p_st_cmd->cmd_type = 4;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_data,&i_cmd_len);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(commit_cmd);
    frfree(p_st_cmd);
    frfree(p_data);

}

//print
void case5(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    PrintCommand *print_cmd = NULL;
    char *p_args = NULL;
    char *p_data = NULL;
    unsigned int i_cmd_len = 0;
    int i_size_tmp = 0;

    //设置p_args
 /*   i_size_tmp = strlen("system") + 1;
    p_args = frmalloc(i_size_tmp);
    snprintf(p_args,i_size_tmp,"%s","system");
*/
    //设置print_cmd
    print_cmd = frmalloc(sizeof(PrintCommand));
    print_command__init(print_cmd);
    print_cmd->n_args = 0;
    print_cmd->args = &p_args;
    i_size_tmp = strlen("ct") + 1;
    print_cmd->cmd = frmalloc(i_size_tmp);
    snprintf(print_cmd->cmd,i_size_tmp,"%s","ct");

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->print_cmd = print_cmd;
    p_st_cmd->cmd_type = 5;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_data,&i_cmd_len);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(print_cmd->cmd);
    frfree(print_cmd);
    frfree(p_st_cmd);
    frfree(p_data);

}

//export_cmd
void case6(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    ExportCommand *export_cmd = NULL;
    char *p_data = NULL;
    unsigned int i_cmd_len = 0;

    //设置export_cmd
    export_cmd = frmalloc(sizeof(ExportCommand));
    export_command__init(export_cmd);
    export_cmd->ip = iptools_parse_ipaddr("192.168.1.1");
    export_cmd->port = 22;

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->export_cmd = export_cmd;
    p_st_cmd->cmd_type = 6;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_data,&i_cmd_len);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    frfree(export_cmd);
    frfree(p_st_cmd);
    frfree(p_data);

}


int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__protobuf_do_pack 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

   	CU_add_test(pSuite, "case2", case2);

   	CU_add_test(pSuite, "case3", case3);

   	CU_add_test(pSuite, "case4", case4);

   	CU_add_test(pSuite, "case5", case5);

   	CU_add_test(pSuite, "case6", case6);

    return 0;

}

int main(int argc, char **argv)
{
    frmem_init();
    run_cunit(add_suite);
    frshow_leak();
    frmem_destroy();
    return 0;
}